﻿<UserControl x:Class="InfFront.InfoViews.WPF.SingleEntryFrame"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:InfFront.InfoViews.WPF"
    xmlns:tk="clr-namespace:Microsoft.Windows.Controls;assembly=WpfToolkit"
    Height="300" Width="300"
    DataContextChanged="this_DataContextChanged">
    
    <!--********************************** Resource *************************************-->
    <UserControl.Resources>
        <local:IconedObjectToNormal24x24ImageConverter x:Key="getNormal24x24Image"/>
        <local:Int32ToTextConverter x:Key="intToText"/>
        <local:FloatToTextConverter x:Key="floatToText"/>
        <local:BoolNotConverter x:Key="BoolNot" />

        <Style x:Key="defaultButtonStyle" TargetType="{x:Type Button}">
            <Setter Property="Margin" Value="4"/>
            <Setter Property="Padding" Value="4"/>
        </Style>
        
        <Style x:Key="EnumComboBoxItemStyle" TargetType="{x:Type ComboBoxItem}">
            <Setter Property="Content" Value="{Binding Path=Title}" />
        </Style>
        
        <DataTemplate x:Key="enumEditorTemplate">
            <ComboBox IsEditable="False" MinWidth="200"
                      IsReadOnly="{Binding Path=IsReadOnly}" 
                      ItemContainerStyle="{StaticResource EnumComboBoxItemStyle}"
                      ItemsSource="{Binding Path=EnumItems}"
                      Loaded="enumComboBox_Loaded"/>
        </DataTemplate>
        
        <DataTemplate x:Key="referenceEditorTemplate">
            <WrapPanel>
                <TextBox Text="{Binding Path=DisplayValue, Mode=OneWay}" 
                         MinWidth="200" IsReadOnly="True" />
                <Button Click="btnSelectReference_Click" 
                        IsEnabled="{Binding Path=IsReadOnly, Converter={StaticResource BoolNot}}">...</Button>
            </WrapPanel>
        </DataTemplate>

        <DataTemplate x:Key="textEditorTemplate">
            <TextBox Text="{Binding Path=Value}" 
                     MinWidth="200"
                     IsReadOnly="{Binding Path=IsReadOnly}" />
        </DataTemplate>

        <DataTemplate x:Key="intEditorTemplate">
            <TextBox Text="{Binding Path=Value, Converter={StaticResource intToText}}" 
                     MinWidth="200"
                     IsReadOnly="{Binding Path=IsReadOnly}" />
        </DataTemplate>

        <DataTemplate x:Key="floatEditorTemplate">
            <TextBox Text="{Binding Path=Value, Converter={StaticResource floatToText}}" 
                     MinWidth="200"
                     IsReadOnly="{Binding Path=IsReadOnly}" />
        </DataTemplate>

        <DataTemplate x:Key="dateEditorTemplate">
            <tk:DatePicker SelectedDate="{Binding Path=Value, Mode=TwoWay}" 
                           MinWidth="200"
                           IsEnabled="{Binding Path=IsReadOnly, Converter={StaticResource BoolNot}}" />
        </DataTemplate>
        
        <DataTemplate x:Key="boolEditorTemplate">
            <CheckBox IsChecked="{Binding Path=Value}"
                      IsEnabled="{Binding Path=IsReadOnly, Converter={StaticResource BoolNot}}" />
        </DataTemplate>

        <DataTemplate x:Key="singleRowDataTemplate">
            <StackPanel Orientation="Horizontal" Margin="4">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition SharedSizeGroup="FieldTitles" />
                        <ColumnDefinition SharedSizeGroup="FieldValues" />
                    </Grid.ColumnDefinitions>
                    <WrapPanel Grid.Column="0" VerticalAlignment="Center">
                        <TextBlock Text="{Binding Path=Title}" />
                        <TextBlock Text=": " />
                    </WrapPanel>
                    <ContentControl Name="anEditor" Grid.Column="1" Content="{Binding}"
                                    ContentTemplateSelector="{DynamicResource editorTemplateSelector}"/>
                </Grid>
            </StackPanel>
        </DataTemplate>
    </UserControl.Resources>

    <!--********************************** Content *************************************-->
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <ItemsControl x:Name="fieldsControlList" Padding="4" 
                      Grid.IsSharedSizeScope="True"
                      VerticalContentAlignment="Center"
                      ItemTemplate="{StaticResource singleRowDataTemplate}" Grid.Row="0">
        </ItemsControl>
    
        <ItemsControl x:Name="fieldGroupsControlList"
                      Grid.IsSharedSizeScope="True" Margin="4" Grid.Row="1">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <GroupBox Header="{Binding Path=Title}" Margin="4">
                        <ItemsControl Padding="4" 
                                      VerticalContentAlignment="Center"
                                      ItemTemplate="{StaticResource singleRowDataTemplate}"
                                      ItemsSource="{Binding Path=Fields}">
                        </ItemsControl>
                    </GroupBox>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

        <Grid Grid.Row="2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <UniformGrid Grid.Column="1" Rows="1">
                <Button Name="btnOk" 
                    Style="{StaticResource defaultButtonStyle}"
                    Click="btnOk_Click">Ok</Button>
                <Button Name="btnCancel" 
                    Style="{StaticResource defaultButtonStyle}" 
                    Click="btnCancel_Click" IsDefault="True" IsCancel="True">Cancel</Button>
                <Button Name="btnApply" 
                    Style="{StaticResource defaultButtonStyle}"
                    Click="btnApply_Click">Apply</Button>
            </UniformGrid>
        </Grid>
    </Grid>
</UserControl>
